package com.ponhu.ea2.stockcenter.provider.service.rpc.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ponhu.ea2.common.basic.model.PagedResult;
import com.ponhu.ea2.stockcenter.dto.StockBatchChangeQueryDTO;
import com.ponhu.ea2.stockcenter.dto.StockBatchQueryDTO;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryBatchRecord;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockInventoryBatchChangeService;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockInventoryBatchRecordService;
import com.ponhu.ea2.stockcenter.provider.utils.ProBeanUtils;
import com.ponhu.ea2.stockcenter.service.bms.StockBatchService;
import com.ponhu.ea2.stockcenter.vo.StockBatchChangeVO;
import com.ponhu.ea2.stockcenter.vo.StockBatchVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@DubboService(version = "1.0.0", interfaceClass = StockBatchService.class, timeout = 60000)
public class StockBatchServiceImpl implements StockBatchService {

    @Autowired
    private StockInventoryBatchRecordService stockInventoryBatchRecordService;
    @Autowired
    private StockInventoryBatchChangeService stockInventoryBatchChangeService;

    @Override
    public PagedResult<StockBatchVO> getStockBatchListByInventoryCode(StockBatchQueryDTO queryDTO) {

        IPage<StockInventoryBatchRecord> iPage = stockInventoryBatchRecordService.getStockBatchListByInventoryCode(queryDTO);

        List<StockBatchVO> stockBatchVOS = iPage.getRecords().parallelStream().map(record ->
                ProBeanUtils.copyProperties(record, StockBatchVO.class)).collect(Collectors.toList());

        return new PagedResult<>(stockBatchVOS, (int) iPage.getCurrent(), iPage.getTotal(), (int) iPage.getSize());
    }

    @Override
    public PagedResult<StockBatchChangeVO> getStockBatchChangeListByQuery(StockBatchChangeQueryDTO queryDTO) {

        // 条件查询分页货品实体信息列表
        IPage<StockBatchChangeVO> iPage = stockInventoryBatchChangeService.getStockBatchChangeListByQuery(queryDTO);

        List<StockBatchChangeVO> changeVOS = iPage.getRecords().stream().map(change ->
                ProBeanUtils.copyProperties(change, StockBatchChangeVO.class)).collect(Collectors.toList());

        // 构建响应数据分页模型
        return new PagedResult<>(changeVOS, (int) iPage.getCurrent(), iPage.getTotal(), (int) iPage.getSize());
    }

}

